package www.openteam.com.ui.business;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;

import java.util.Calendar;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.base.OtherBaseFragment;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.ApprovalStatu;
import www.openteam.com.bean.CompleteMainCustomOrderInfo;
import www.openteam.com.bean.CustomType;
import www.openteam.com.bean.PackTypeInfo;
import www.openteam.com.bean.PbProductInfo;
import www.openteam.com.bean.SomeCustomInfo;
import www.openteam.com.contract.business.EditCustomOrderInfoContract;
import www.openteam.com.presenter.business.NewBuildCustomOrderInfoPresenter;
import www.openteam.com.tool.TimeTool;
import www.openteam.com.tool.ToastUtil;
import www.openteam.com.ui.DetailedActivity;


/**
 * Created by kylin on 2016/10/27.
 */

public class BuildCustomOrderMainFragment extends OtherBaseFragment<NewBuildCustomOrderInfoPresenter>
        implements EditCustomOrderInfoContract.EditCustomOrderInfoView {

    private static final String TAG = BuildCustomOrderMainFragment.class.getSimpleName();
    private ButtonInterface buttonInterface;
    private int year;
    private int monthOfYear;
    private int dayOfMonth;
    @Bind(R.id.btn_insert_main_ensure)
    Button btn_insert_main_ensure;
    @Bind(R.id.btn_insert_main_cancel)
    Button btn_insert_main_cancel;
    @Bind(R.id.btn_modify_ensure)
    Button btn_modify_ensure;
    @Bind(R.id.btn_empty)
    Button btn_empty;
    LinearLayout ll_main_et;
    RelativeLayout rl_detailorder;
    @Bind(R.id.sp_order_istax)
    Spinner sp_order_istax;
    @Bind(R.id.et_order_custorderid)
    EditText et_order_custorderid;
    @Bind(R.id.et_order_customno)
    EditText et_order_customno;
    @Bind(R.id.et_order_customname)
    EditText et_order_customname;
    @Bind(R.id.et_order_moneyname)
    EditText et_order_moneyname;
    @Bind(R.id.et_order_payname)
    EditText et_order_payname;
    @Bind(R.id.et_order_sendname)
    EditText et_order_sendname;
    @Bind(R.id.et_salesman)
    EditText et_salesman;
    @Bind(R.id.et_order_alreadlypay)
    EditText et_order_alreadlypay;
    @Bind(R.id.et_order_infoamout)
    @Nullable
    EditText et_order_infoamout;
    @Bind(R.id.et_order_inputdate)
    EditText et_order_inputdate;
    @Bind(R.id.et_order_orderdate)
    EditText et_order_orderdate;
    @Bind(R.id.et_order_takegoods)
    EditText et_order_takegoods;
    @Bind(R.id.et_order_taxrate)
    EditText et_order_taxrate;
    @Bind(R.id.et_order_invtype)
    EditText et_order_invtype;
    @Bind(R.id.et_order_stype)
    EditText et_order_stype;
    @Bind(R.id.et_order_remark)
    EditText et_order_remark;
    @Bind(R.id.et_order_dproductname)
    EditText et_order_dproductname;
    @Bind(R.id.et_order_dproductno)
    EditText et_order_dproductno;
    @Bind(R.id.et_order_dproductprice)
    EditText et_order_dproductprice;
    @Bind(R.id.et_order_dproductnumb)
    EditText et_order_dproductnumb;
    @Bind(R.id.et_order_dsenddate)
    EditText et_order_dsenddate;
    @Bind(R.id.et_order_wraptype)
    EditText et_order_wraptype;
    @Bind(R.id.et_order_paperinf)
    EditText et_order_paperinf;
    @Bind(R.id.et_order_technicsinf)
    EditText et_order_technicsinf;
    @Bind(R.id.et_order_onum)
    EditText et_order_onum;
    @Bind(R.id.et_order_printcolor)
    EditText et_order_printcolor;

    private int id;

    private CompleteMainCustomOrderInfo completeMainCustomOrderInfo =
            new CompleteMainCustomOrderInfo();


    @Override
    public int getLayoutId() {
        return R.layout.fragment_build_order;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        et_order_customno.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonInterface.onclickSearchCustom();
            }
        });


        et_order_moneyname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonInterface.onclickSearchMoneyName();
            }
        });

        et_order_payname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonInterface.onclickSearchCopay();
            }
        });
        et_order_sendname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonInterface.onclickSearchSendName();
            }
        });
        et_order_inputdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectCalendar((View) v.getParent().getParent().getParent(), 0);
            }
        });

        et_order_orderdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectCalendar((View) v.getParent().getParent().getParent(), 1);
            }
        });
    }

    @Override
    protected void initView() {

        et_order_inputdate.setText(TimeTool.getCurrentTiem());
        ll_main_et = (LinearLayout) view.findViewById(R.id.ll_main_et);
        rl_detailorder = (RelativeLayout) view.findViewById(R.id.rl_detailorder);
    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected NewBuildCustomOrderInfoPresenter loadPresenter() {
        return new NewBuildCustomOrderInfoPresenter();
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        btn_insert_main_ensure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                mPresenter.insertCustomOrderMainInfo(
                        getCustid(),
                        getBusinessno(), getUserid(),
                        getMoneytype(),
                        getPaytime(),
                        getSendtype(),
                        getCustorderid(),
                        getAlreadlypay(),
                        getInputdate(),
                        getInputer(), getOrderamount(),
                        getRemark(),
                        getOrderdate(),
                        getSendaddress(),
                        getIstax(),
                        getTaxrate(),
                        getInvtype(),
                        getStype());
            }
        });
//        Log.d(TAG, "onCreateView:  3");


        btn_modify_ensure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                mPresenter.insertCustomOrderItemInfo(
                        id,
                        et_order_dproductno.getText().toString(),
                        et_order_wraptype.getText().toString()
                        , et_order_paperinf.getText().toString()
                        , et_order_dsenddate.getText().toString(),
                        et_order_technicsinf.getText().toString(),
                        et_order_printcolor.getText().toString(),
                        et_order_dproductnumb.getText().toString(),
                        et_order_onum.getText().toString(),
                        et_order_dproductprice.getText().toString()
                );

            }
        });
    }


    public boolean checkMainNull() {
        boolean isNull = false;
        if (et_order_custorderid.getText().toString().isEmpty()) {
            isNull = true;
            et_order_custorderid.setError("客户单号不能为空");
        } else if (et_order_customno.getText().toString().isEmpty()) {
            isNull = true;
            et_order_customno.setError("客户编号不能为空");
        } else if (et_order_moneyname.getText().toString().isEmpty()) {
            isNull = true;
            et_order_moneyname.setError("币别类型不能为空");
        } else if (et_order_payname.getText().toString().isEmpty()) {
            isNull = true;
            et_order_payname.setError("支付类型不能为空");
        } else if (et_order_sendname.getText().toString().isEmpty()) {
            isNull = true;
            et_order_sendname.setError("寄送类型不能为空");
        } else if (et_salesman.getText().toString().isEmpty()) {
            isNull = true;
            et_salesman.setError("业务员不能为空");
        } else if (et_order_alreadlypay.getText().toString().isEmpty()) {
            isNull = true;
            et_order_alreadlypay.setError("已支付金额不能为空");
        } else if (et_order_takegoods.getText().toString().isEmpty()) {
            isNull = true;
            et_order_takegoods.setError("送货地址不能为空");
        } else if (et_order_inputdate.getText().toString().isEmpty()) {
            isNull = true;
            et_order_inputdate.setError("输入日期不能为空");
        } else if (et_order_orderdate.getText().toString().isEmpty()) {
            isNull = true;
            et_order_orderdate.setError("订货日期不能为空");
        }

        if (!isNull) {
            createProgressDialog(getResources().getString(R.string.waiting));
        }
        return isNull;
    }

    public boolean checkItemNull() {
        boolean isNull = false;
        if (getProdno().isEmpty()) {
            isNull = true;
            et_order_dproductno.setError("产品编号不能为空");
        } else if (getPrice().isEmpty()) {
            isNull = true;
            et_order_dproductprice.setError("价格不能为空");
        } else if (getPaynumb().isEmpty()) {
            isNull = true;
            et_order_dproductnumb.setError("数量不能为空");
        } else if (getSenddate().isEmpty()) {
            isNull = true;
            et_order_dsenddate.setError("送货日期不能为空");
        }
        if (!isNull) {
            createProgressDialog(getResources().getString(R.string.waiting));
        }
        return isNull;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        ((DetailedActivity) getActivity()).searchNewFragmentClickEvent(new DetailedActivity.SearchNewFragmentButton() {


            @Override
            public void searchNewFramentButtonClick(PbProductInfo.DataEntity data, String info) {
                et_order_dproductname.setText(data.getPbName());
                et_order_dproductno.setText(data.getCode());
                et_order_technicsinf.setText(data.getTechnicsinf());
                et_order_paperinf.setText(data.getPaperInf());

            }

            @Override
            public void searchNewPackTypeInfoClick(PackTypeInfo.DataEntity dataEntity, String info) {
                et_order_wraptype.setText(dataEntity.getName());
            }

            @Override
            public void searchNewCustomInfoClick(SomeCustomInfo.DataEntity dataEntity) {
//                this.customs=dataEntity;
                completeMainCustomOrderInfo.setCustid(dataEntity.getCode());
                completeMainCustomOrderInfo.setCustname(dataEntity.getName());
                completeMainCustomOrderInfo.setStype(dataEntity.getType());
                completeMainCustomOrderInfo.setPaytime(dataEntity.getPaytime());
                completeMainCustomOrderInfo.setMoneytype(dataEntity.getMoneytype());
                et_order_customno.setText(dataEntity.getCode());
                et_order_customname.setText(dataEntity.getName());
                et_order_stype.setText(dataEntity.getTypename());
                et_order_payname.setText(dataEntity.getPayname());
                et_order_moneyname.setText(dataEntity.getMoneyname());

            }

            @Override
            public void searchNewComtomTypeInfoClick(CustomType.DataBean customType, int type) {
                switch (type) {
                    case 0:
                        et_order_moneyname.setText(customType.getName());
                        completeMainCustomOrderInfo.setPaytime(customType.getCode());
                        break;
                    case 1:
                        et_order_payname.setText(customType.getName());
                        completeMainCustomOrderInfo.setPaytime(customType.getCode());
                        break;
                    case 2:
                        et_order_stype.setText(customType.getName());
                        completeMainCustomOrderInfo.setPaytime(customType.getCode());
                        break;
                    case 3:
                        et_order_sendname.setText(customType.getName());
                        completeMainCustomOrderInfo.setSendtype(customType.getCode());
                        break;
                }
            }


        });
    }


//    private void initView(View view) {
//        Log.d(TAG, "initView: 4");
//        product_type = (Spinner) view.findViewById(R.id.product_type);
//        productno = (TextView) view.findViewById(R.id.productno);
//        productname = (TextView) view.findViewById(R.id.productname);
//        customno_product = (TextView) view.findViewById(R.id.customno_product);
//        length_product = (TextView) view.findViewById(R.id.length_product);
//        width_product = (TextView) view.findViewById(R.id.width_product);
//        height_product = (TextView) view.findViewById(R.id.height_product);
//        unit_product_new = (TextView) view.findViewById(R.id.unit_product_new);
//        text_product = (TextView) view.findViewById(R.id.text_product);
//        conver_product = (TextView) view.findViewById(R.id.conver_product);
//        maxstock = (TextView) view.findViewById(R.id.maxstock);
//        minstock = (TextView) view.findViewById(R.id.minstock);
//        returnButton1 = (Button) view.findViewById(R.id.returnButton1);
//        product_type.setAdapter(materialAdapter);
//
//    }


    /**
     * 初始化日历时间
     */
    private void initCalendar() {
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        monthOfYear = calendar.get(Calendar.MONTH);
        dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        Time time = new Time("GMT+8");
        time.setToNow();
    }

    public void selectCalendar(final View tview, final int type) {
        initCalendar();

        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                EditText editText;
                switch (type) {
                    case 0:
                        editText = (EditText) tview.findViewById(R.id.et_order_inputdate);
                        break;
                    case 1:
                        editText = (EditText) tview.findViewById(R.id.et_order_orderdate);
                        break;
                    default:
                        editText = (EditText) tview.findViewById(R.id.et_order_inputdate);
                        break;
                }

                editText.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
            }

        }, year, monthOfYear, dayOfMonth);

        datePickerDialog.show();
    }

    /**
     * 按钮点击事件需要的方法
     */

    public void buttonSetOnclick(ButtonInterface buttonInterface) {
        this.buttonInterface = buttonInterface;
    }

    @Override
    public String getProdno() {

        return et_order_dproductno.getText().toString();
    }

    @Override
    public String getWraptype() {

        return et_order_wraptype.getText().toString();
    }

    @Override
    public String getPaperinf() {

        return et_order_paperinf.getText().toString();
    }

    @Override
    public String getSenddate() {
        return et_order_dsenddate.getText().toString();
    }

    @Override
    public String getTechnicsinf() {
        return et_order_technicsinf.getText().toString();
    }

    @Override
    public String getPrintcolor() {
        return et_order_printcolor.getText().toString();
    }

    @Override
    public String getPaynumb() {
        return et_order_dproductnumb.getText().toString();
    }

    @Override
    public String getOthernumb() {
        return et_order_onum.getText().toString();
    }

    @Override
    public String getPrice() {

        return et_order_dproductprice.getText().toString();
    }

    @Override
    public String getCustid() {
        return completeMainCustomOrderInfo.getCustid();
    }

    @Override
    public String getBusinessno() {
        return et_salesman.getText().toString();
    }

    @Override
    public String getUserid() {
        return mySetting.getInt("mId", 0) + "";
    }

    @Override
    public String getMoneytype() {
        return completeMainCustomOrderInfo.getMoneytype();
    }

    @Override
    public String getPaytime() {
        return completeMainCustomOrderInfo.getPaytime();
    }

    @Override
    public String getSendtype() {
        return completeMainCustomOrderInfo.getSendtype();
    }

    @Override
    public String getCustorderid() {
        return et_order_custorderid.getText().toString();
    }

    @Override
    public String getAlreadlypay() {
        return et_order_alreadlypay.getText().toString();
    }

    @Override
    public String getInputdate() {
        return et_order_inputdate.getText().toString();
    }

    @Override
    public String getInputer() {
        return mySetting.getString("mName", "");
    }

    @Override
    public String getOrderamount() {
        return "0";
    }

    @Override
    public String getRemark() {
        return et_order_remark.getText().toString();
    }

    @Override
    public String getOrderdate() {
        return et_order_orderdate.getText().toString();
    }

    @Override
    public String getSendaddress() {
        return et_order_takegoods.getText().toString();
    }

    @Override
    public String getIstax() {
        return sp_order_istax.getSelectedItemPosition() + "";
    }

    @Override
    public String getTaxrate() {
        return et_order_taxrate.getText().toString();
    }

    @Override
    public String getInvtype() {
        return et_order_invtype.getText().toString();
    }

    @Override
    public String getStype() {
        return mySetting.getString("rCode", "01");
    }

    @Override
    public void insertCOIData(ApprovalStatu approvalStatu) {
        mProgressDialog.dismiss();
        ll_main_et.setVisibility(View.GONE);
        rl_detailorder.setVisibility(View.VISIBLE);
        id = approvalStatu.getData();

        et_order_dproductname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonInterface.onclickSearch();
            }
        });

        et_order_wraptype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonInterface.onclickSearchPackType();
            }
        });
    }


    @Override
    public void hideCOIData(Throwable throwable) {
        mProgressDialog.dismiss();
        ToastUtil.showToast(getActivity(), "新建失败!");
    }

    @Override
    public void insertCOIIData(ApprovalStatu approvalStatu) {
        if (null != mProgressDialog) {
            mProgressDialog.dismiss();
        }
        final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(getActivity());
        builder.setTitle("提示");
        builder.setMessage("是否继续新建子订单");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                buttonInterface.insertSucces();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                et_order_dproductname.setText("");
                et_order_dproductno.setText("");
                et_order_dproductprice.setText("");
                et_order_dproductnumb.setText("");
                et_order_stype.setText("");
                et_order_payname.setText("");
                et_order_moneyname.setText("");
                et_order_wraptype.setText("");
                et_order_paperinf.setText("");
                et_order_technicsinf.setText("");
                et_order_printcolor.setText("");
                et_order_onum.setText("");
                et_order_dsenddate.setText("");
                dialog.dismiss();
            }
        });
        builder.show();

    }

    @Override
    public void hideCOIIData(Throwable throwable) {
        if (null != mProgressDialog) {
            mProgressDialog.dismiss();
        }
        ToastUtil.showToast(getActivity(), "新建失败!");
    }


    @Override
    public void onRefresh() {

    }

    /**
     * 按钮点击事件对应的接口
     */
    public interface ButtonInterface {

        void onclickSearch();

        void onClickNewBuild();

        void insertSucces();

        void onclickSearchPackType();

        void onclickSearchCustom();

        void onclickSearchCopay();

        void onclickSearchMoneyName();

        void onclickSearchSendName();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        completeMainCustomOrderInfo = null;
    }


}
